<?php
namespace app\admin\controller;
use think\Db;
class Admingroup extends Base
{
    #没有在菜单里的权限,写在这儿
    public $url = [
        "/adminuser/index" => "后台首页",
        "/deploy/group" => "网站配置",
    ];
    //列表
    public function lists(){
        if(is_post()){
            $d = request()->post();
            $map = [];
            if($d['keyword']!=''){
                $map[] = ['title','like',"%{$d['keyword']}%"];
            }
        	$rs = model('AdminGroup')::where($map)
                                ->with(["adminUser"])
                                ->order('id desc')
                                ->paginate($d['limit'],false,['page'=>$d['page']]);
            if($rs){
                $rs = $rs->toArray();
                return suc($rs['data'], $rs['total']);
            }else{
                return suc([],0);
            }
        }
        return $this->fetch();
    }
    //添加
    public function add(){
        if(is_post()){
            $d = request()->post();
            $d['content'] = implode(",",array_unique(array_filter($d['content'])));
            if(model('AdminGroup')->renew($d)){
                session('js.function','preload');
                return suc();
            }else{
                return err(model('AdminGroup')->getError());
            }
        }
        $menu = c('adminmenu.');
        $arr_id = admin_menu_arr($menu,0);
        $tree   = admin_menu_tree($arr_id,0);
        return $this->fetch('',['tree'=>$tree,'right_url'=>$this->url]);
    }
    //编辑
    public function edit($id=''){
        if(is_post()){
            $d = request()->post();
            $d['content'] = implode(",",array_unique(array_filter($d['content'])));
            if(model('AdminGroup')->renew($d)){
                session('js.function','preload');
                return suc();
            }else{
                return err(model('AdminGroup')->getError());
            }
        }
        $info = model('AdminGroup')->get($id);
        $menu = c('adminmenu.');
        $arr_id = admin_menu_arr($menu,0);
        $tree   = admin_menu_tree($arr_id,0);
        $content = explode(",",$info['content']);
        return $this->fetch('',['info'=>$info,'tree'=>$tree, "content"=>$content,'right_url'=>$this->url]);
    }
    //删除
    public function del(){
        if(is_post()){
            $d = input();
            $rs = model('AdminGroup')->del($d['id']);
            session('js.function','reload');
            if($rs){
                return suc();
            }else{
                return err(model('AdminGroup')->getError());
            }
        }
    }
    //详情
    public function detail($id){
        return $this->fetch();
    }
}
